package org.qingke.mail.user.service.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;

/**
 * Spring boot 与 mybatis 集成配置类
 * @author JAVA
 *
 */
@Configuration
@PropertySource("classpath:mysql.properties")
@EnableTransactionManagement
public class SpringBootMybatisConfig implements TransactionManagementConfigurer {
	
	@Value("${mysqlUrl}")
	private String mysqlUrl;
	
	@Value("${mysqlDriver}")
	private String mysqlDriver;
	
	@Value("${mysqlUsername}")
	private String mysqlUsername;
	
	@Value("${mysqlPassword}")
	private String mysqlPassword;

	@Autowired
	private DataSource dataSource;
	
	// 配置数据源
	@Bean
	public DataSource getDataSource(){
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setUrl(mysqlUrl);
		druidDataSource.setDriverClassName(mysqlDriver);
		druidDataSource.setUsername(mysqlUsername);
		druidDataSource.setPassword(mysqlPassword);
		return druidDataSource;
	}
	
	// 配置SQLSessionFactory
	@Bean("sqlSessionFactory")
	public SqlSessionFactoryBean getSqlSessionFactoryBean(){
		
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
		sqlSessionFactoryBean.setTypeAliasesPackage("org.qingke.mail.domain");
		//分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
		return sqlSessionFactoryBean;
	}
	
	/**
	 * 创建SqlSession操作模板
	 * @param sqlSessionFactory
	 * @return
	 */
	@Bean
	public SqlSessionTemplate getSqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
		return new SqlSessionTemplate(sqlSessionFactory);
	}
	
	/**
	 * 创建事物管理器并设置注解驱动事物
	 */
	@Override
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}

	public String getMysqlUrl() {
		return mysqlUrl;
	}

	public void setMysqlUrl(String mysqlUrl) {
		this.mysqlUrl = mysqlUrl;
	}

	public String getMysqlDriver() {
		return mysqlDriver;
	}

	public void setMysqlDriver(String mysqlDriver) {
		this.mysqlDriver = mysqlDriver;
	}

	public String getMysqlUsername() {
		return mysqlUsername;
	}

	public void setMysqlUsername(String mysqlUsername) {
		this.mysqlUsername = mysqlUsername;
	}

	public String getMysqlPassword() {
		return mysqlPassword;
	}

	public void setMysqlPassword(String mysqlPassword) {
		this.mysqlPassword = mysqlPassword;
	}

}
